// packageF/others/customerCenter/component/userDetail/orderList/orderList.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    keywork: {
      type: null
    },
    memberId: {
      type: null
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    typeIndex: '',
    detailUrl: 'orderdetail',
    orderType: 'shop',
    firstRquery: false,
    json: {
      page: 1,
      keyword: '',
      order_type: 'order',
      status: '1',
    },
    loading: false,
    finished: false,

    order_tabs: [],
    orderList: [],
    orderStatusTabs: [],
    strUrl: 'plugin.wechat-chat-sidebar.frontend.controller.customer.customer.get-order-list',
    getConfig: {
      client: 'work',
      type: 17
    }, //企业微信公共参数
  },
  lifetimes: {
    attached() {
      this.getData();
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    getData() {
      if (this.data.finished && this.data.loading) {
        return;
      }
      let getObj = {};
      if (this.data.json.order_type === "refund_order") {
        if (this.data.keywork == 'customer') {
          getObj = {
            page: this.data.json.page,
            keyword: this.data.json.keyword,
          };
          this.data.strUrl = 'plugin.customer-center.frontend.controllers.customer.refund-apply';
          getObj.member_id = this.data.memberId;
        }
      } else {
        if (this.data.keywork == 'customer') {
          getObj = {
            ...this.data.json,
          };
          this.data.strUrl = 'plugin.customer-center.frontend.controllers.customer.get-order-list';
          getObj.member_id = this.data.memberId;
        }
      }

      this.data.loading = true;

      let urlStr = app.getNetAddresss(this.data.strUrl);
      app._postNetWork({
        url: urlStr,
        data: getObj,
        success: (resdata) => {
          let res = resdata.data
          if (res.result == 1) {
            console.log(res, 'ddddddddd')
            if (this.data.json.page >= res.data.last_page || res.data.data.length < res.data.per_page) {
              this.setData({
                finished: true
              })
            }
            if (this.data.order_tabs.length <= 0) {
              this.data.order_tabs = this.setOrderTabs(res.data);
              this.data.orderStatusTabs = this.data.order_tabs[0].list;
              this.setData({
                order_tabs: this.data.order_tabs,
                orderStatusTabs: this.data.orderStatusTabs
              })
            }
            if (res.data) {
              this.setData({
                orderList: this.data.json.page > 1 ? this.data.orderList.concat(res.data.data) : res.data.data,
                'json.page': ++this.data.json.page
              })
            }
          } else {
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    },
    setOrderTabs(data) {
      if (!data.order_name && !data.order) {
        return;
      }

      let NavList = [{ status: 1, status_name: "全部" }, { status: 2, status_name: "待付款" }, { status: 3, status_name: "待发货" }, { status: 4, status_name: "待收货" }, { status: 5, status_name: "已完成" }];
      // 处理订单列表
      let tablist = [];
      for (let order_key in data.order.json) {
        let flag = true;
        let title = data.order_name[order_key];
        if (order_key) {
          if (order_key === "order") {
            data.order[order_key] = NavList;
          } else if (order_key === "hotel_order") {
            data.order[order_key] = [{ status: 1, status_name: "全部" }, { status: 2, status_name: "待付款" }, { status: 3, status_name: "待确认" }, { status: 4, status_name: "待入住" }, { status: 5, status_name: "已完成" }, { status: 6, status_name: "待退房" }];
          } else if (order_key === "lease_order") {
            data.order[order_key] = [{ status: 1, status_name: "全部" }, { status: 2, status_name: "待支付" }, { status: 3, status_name: "待发货" }, { status: 4, status_name: "待收货" }, { status: 5, status_name: "待归还" }, { status: 6, status_name: "已完成" }];
          } else if (order_key === "fight_groups_order") {
            data.order[order_key] = [{ status: 1, status_name: "全部" }, { status: 2, status_name: "拼团中" }, { status: 3, status_name: "待付款" }, { status: 4, status_name: "待收货" }, { status: 5, status_name: "已完成" }];
          } else if (order_key === "snatch_regiment_order") {
            data.order[order_key] = [{ status: 1, status_name: "抢团中" }, { status: 2, status_name: "待付款" }, { status: 2, status_name: "待发货" }, { status: 3, status_name: "待收货" }, { status: 5, status_name: "已完成" }];
          } else {
            // 其他先不做
            flag = false;
          }
          // else if (order_key === "live_install_order"){
          //   data.order[order_key] = [{status: -1,status_name: "全部"},  {status: 1,status_name: "待支付"},{status: 2,status_name: "待分配"}, {status: 3,status_name: "待取货"}, {status: 4,status_name: "待到付"}, {status: 4,status_name: "待安装"}, {status: 5,status_name: "已完成"}];
          // }
          if (flag) {
            tablist.push({
              name: order_key,
              title: title,
              list: data.order[order_key]
            });
          }

        }
      }
      tablist.push({
        name: "refund_order",
        title: "售后订单",
        list: [],
      });
      return tablist;
    },
    loadMore() {
      this.getData();
    },
    //订单类型
    changeOrderType(e) {
      console.log(e)
      console.log(this.data.order_tabs[e.detail],'ddddddd')
      this.setData({
        typeIndex: e.detail,
        'json.status': 1,
        'json.order_type': this.data.order_tabs[e.detail].name,
        orderStatusTabs: this.data.order_tabs[e.detail].list
      })
      if (this.data.json.order_type === 'order') {
        this.data.detailUrl = "orderdetail";
        this.data.orderType = "shop";
      } else if (this.data.json.order_type === 'hotel_order') {
        this.data.detailUrl = "HotelOrderDetail";
        this.data.orderType = "hotel";
      } else if (this.data.json.order_type === 'lease_order') {
        this.data.detailUrl = "LeaseToyOrderDetail";
        this.data.orderType = "lease_toy";
      } else if (this.data.json.order_type === 'fight_groups_order') {
        this.data.detailUrl = "groupDetail";
        this.data.orderType = "groups";
      } else if (this.data.json.order_type === 'snatch_regiment_order') {
        this.data.detailUrl = "orderdetail";
        this.data.orderType = "grabGroup";
      }
      this.setData({
        orderType: this.data.orderType,
        detailUrl: this.data.detailUrl,
      })
      this.getParamInit();
      this.getData();
    },
    //订单状态
    changeOrderStatus(e) {
      console.log(e)
      this.setData({
        'json.status': e.detail.name
      })
      this.getParamInit();
      this.getData();
    },
    search(e) {
      this.setData({
        'json.keyword': e.detail,
        loading: false,
        finished: false,
        'json.page':1
      })
      this.getData()
    },
    getParamInit() {
      this.setData({
        orderList: [],
        'json.page': 1,
        'json.keyword': '',
        loading: false,
        finished: false
      })
    },
  }
})